<template>
  <div class="auditCmpt">
    <!-- {{carrier}} -->
    <!-- 查询表单 -->
    <div class="form-baseinfo">
      <LPSearchBar
        v-model="searchModel"
        :search-data="searchData"
        label-width="100px"
        @handleSearchSubmit="handleSearchSubmit"
        @handleSearchReset="handleSearchReset"
      />
    </div>
    <!-- 表格列表 -->
    <div class="form-baseinfo">
      <div class="btn_area">
        <el-button size="medium" type="primary" @click="exportInfo">导出数据</el-button>
      </div>
      <LPTablePage
        :show-index="false"
        :show-indexfixed="false"
        :table-data="tableData"
        :table-column="tableColumn"
        :total="total"
        :current-page="currentPage"
        align="center"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getEmployeeConsume } from '@/api/LaborProtection/employee'
import { GetEmployeeConsumeStatisticsExcel } from '@/api/LaborProtection/export'
import { download, parseTime } from '@/utils/lpUtils'
import LPTablePage from '@/components/LPTablePage/index.vue'
import LPSearchBar from '@/components/LPSearchBar/index.vue'

export default {
  name: 'Report',
  components: { LPTablePage, LPSearchBar },
  data() {
    return {
      searchModel: {},
      searchData: [
        { label: '', placeholder: '员工姓名', prop: 'realName', width: '80px' },
        { label: '', placeholder: '部门', prop: 'departmentName' },
        { label: '', placeholder: '岗位', prop: 'jobName' },
        {
          label: '',
          prop: 'StartOrderTime',
          type: 'datetimerange',
          placeholder: '开始时间',
          class: 'BeginTime',
          width: '80px'
        }
      ],
      // 列表数据
      tableData: [],
      // 表格字段
      tableColumn: [
        { label: '员工姓名', prop: 'realName' },
        { label: '工号', prop: 'jobNum' },
        { label: '卡号', prop: 'cardNo' },
        { label: '部门', prop: 'departmentName' },
        { label: '岗位', prop: 'jobName' },
        { label: '消费金额', prop: 'ordersTotalMoney' },
        { label: '退货金额', prop: 'returnTotalMoney' }
      ],
      total: 0,
      currentPage: 1,
      pageSize: 10
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    insertData() {
      this.$router.push({
        path: '/laborProtection/order/returnOrderManage/insertReOrder'
      })
    },
    // 查询按钮
    handleSearchSubmit() {
      this.currentPage = 1
      this.getList()
    },
    // 重置按钮
    handleSearchReset() {
      this.handleSearchSubmit()
    },
    handleSizeChange(v) {
      this.pageSize = v
      this.currentPage = 1
      this.getList()
    },
    handleCurrentChange(v) {
      this.currentPage = v
      this.getList()
    },
    exportInfo() {
      const params = {
        ...this.searchModel
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
        this.$delete(params, 'StartOrderTime')
      }
      GetEmployeeConsumeStatisticsExcel(params).then((res) => {
        download(res, '员工消费统计')
      })
    },
    getList() {
      const params = {
        ...this.searchModel,
        SkipCount: this.currentPage,
        MaxResultCount: this.pageSize
      }
      if (typeof this.searchModel.StartOrderTime !== 'undefined') {
        params.SDateTime = parseTime(this.searchModel.StartOrderTime[0])
        params.EDateTime = parseTime(this.searchModel.StartOrderTime[1])
        this.$delete(params, 'StartOrderTime')
      }
      getEmployeeConsume(params).then((res) => {
        if (!res.error) {
          this.tableData = res.items
          this.total = res.totalCount
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.auditCmpt {
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
  .form-baseinfo:first-child {
    margin: 20px, 0, 20px, -10px;
    border-radius: 3px;
  }
  .form-baseinfo:last-child {
    background-color: #fff;
    .btn_area {
      padding: 0.2rem;
      border-top-left-radius: 3px;
      border-top-right-radius: 3px;
    }
  }
}

::v-deep .search-bar input.el-input__inner {
  width: 2rem;
}
</style>
